<!DOCTYPE html>
<html lang="zxx">

<head>
    <title>登录界面</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta charset="UTF-8">
    <meta name="keywords" content="登录表单">
    <link href="//fonts.googleapis.com/css2?family=Noto+Sans+JP:wght@400;500;700;900&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="css/style.css" type="text/css" media="all">
    <link rel="stylesheet" href="css/font-awesome.min.css" type="text/css" media="all">
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
</head>

<body>
<section class="w3l-hotair-form">
    <h1>登录页面</h1>
    <div class="container">
        <div class="workinghny-form-grid">
            <div class="main-hotair">
                <div class="content-wthree">
                    <h2>登录</h2>
                    <form id="loginForm">
                        <input type="text" class="text" name="username" placeholder="用户名" required autofocus>
                        <input type="password" class="password" name="password" placeholder="密码" required autofocus>
                        <button class="btn" type="submit">登录</button>
                    </form>
                    <p class="account">没有账户？ <a href="register.html" id="registerLink">注册</a></p>
                </div>
                <div class="w3l_form align-self">
                    <div class="left_grid_info">
                        <img src="images/1.png" alt="登录图片" class="img-fluid">
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
<script>
    $(document).ready(function() {
        $('#loginForm').on('submit', function(event) {
            event.preventDefault();

            var name = $('input[name="username"]').val();
            var password = $('input[name="password"]').val();

            $.ajax({
                url: 'http://localhost:8080/Servlet/login',
                method: 'POST',
                data: {
                    username: name,
                    password: password
                },
                success: function(response) {
                    alert(response); // 显示登录成功或失败的消息
                    if (response === "登录成功") {
                        window.location.href = 'http://localhost:8080/Servlet/Log_HTML/home.html'; // 重定向到 home.html
                    }
                },
                error: function(xhr, status, error) {
                    if (xhr.status === 401) {
                        alert('登录失败，请检查用户名和密码');
                    } else {
                        alert('请求失败，请重试');
                    }
                }
            });
        });
    });
</script>
</body>
</html>
